{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "14d07394",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Zhang Ziyang', 'ZHANG ZIYANG', 'zhang ziyang')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "######################################字符串\n",
    "#title首字母大写,其他小写\n",
    "name = \"zhang ziYang\"\n",
    "name.title(), name.upper(), name.lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9fc3220e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zhangziyang'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#拼接\n",
    "first_nm = \"zhang\"\n",
    "last_nm=\"ziyang\"\n",
    "first_nm+last_nm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "195baf09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\tziyang\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#制表符\n",
    "print(\"\\tziyang\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7dfd85a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(' zhang ziyang', 'zhang ziyang')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去除左或右空格\n",
    "name=\" zhang ziyang\\t\"\n",
    "name.rstrip(), name.strip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "424ec8a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'happy 23 years old'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#########################################数字\n",
    "#python不能自动转型\n",
    "#\"happy \" + 23 + \" years old\"\n",
    "\"happy \" + str(23) + \" years old\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "11e77d48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "永久\n"
     ]
    }
   ],
   "source": [
    "#######################################列表\n",
    "bycicle = ['永久','美利达','迪卡侬']\n",
    "print(bycicle[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c4c412c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'迪卡侬'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#指定最后一个元素\n",
    "bycicle[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "66622b45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['永久', '凤凰', '美利达', '迪卡侬', '捷安特']\n"
     ]
    }
   ],
   "source": [
    "#插入\n",
    "bycicle.append('捷安特')\n",
    "bycicle.insert(1, '凤凰')\n",
    "print(bycicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "13501698",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['永久', '凤凰', '美利达', '迪卡侬']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除\n",
    "del bycicle[-1]\n",
    "bycicle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "829d765d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "迪卡侬 ['永久', '凤凰', '美利达']\n"
     ]
    }
   ],
   "source": [
    "#弹出\n",
    "bike = bycicle.pop()\n",
    "print(bike,bycicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6dace943",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "凤凰 ['永久', '美利达']\n"
     ]
    }
   ],
   "source": [
    "#弹出任意位置\n",
    "bike = bycicle.pop(1)\n",
    "print(bike,bycicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3bac8ec3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['永久', '美利达', '永久']\n",
      "['美利达', '永久']\n"
     ]
    }
   ],
   "source": [
    "#根据值进行删除，删除第一个\n",
    "bycicle.append('永久')\n",
    "print(bycicle)\n",
    "bycicle.remove('永久')\n",
    "print(bycicle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f32bfd71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['audi', 'benz', 'bmw', 'toyota']\n",
      "['toyota', 'bmw', 'benz', 'audi']\n"
     ]
    }
   ],
   "source": [
    "#排序,永久改变\n",
    "cars = ['bmw','audi','toyota','benz']\n",
    "cars.sort()\n",
    "print(cars)\n",
    "cars.sort(reverse=True)\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3a3a40c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['toyota', 'bmw', 'benz', 'audi'] ['bmw', 'audi', 'toyota', 'benz']\n"
     ]
    }
   ],
   "source": [
    "#排序函数，不改变原顺序\n",
    "cars = ['bmw','audi','toyota','benz']\n",
    "print(sorted(cars, reverse=True), cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c7fff902",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bmw', 'audi', 'toyota', 'benz']\n",
      "['benz', 'toyota', 'audi', 'bmw']\n"
     ]
    }
   ],
   "source": [
    "#列表倒序，永久修改\n",
    "print(cars)\n",
    "cars.reverse()\n",
    "print(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8a6dec56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "63b2ec73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "benz\n",
      "toyota\n",
      "audi\n",
      "bmw\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "##################################################列表操作\n",
    "for car in cars:\n",
    "    print(car)\n",
    "for val in range(1,5):\n",
    "    print( val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b1659ccc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 3], 1, 3, 4)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = list(range(1,5,2))\n",
    "nums,min(nums),max(nums),sum(nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "2d2147ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表解析\n",
    "squares = [val ** 2 for val in range(1,11)]\n",
    "squares"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "0b4f96ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([4, 9, 16], [1, 4, 9, 16], [64, 81, 100], [64, 81])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片\n",
    "squares[1:4],squares[:4],squares[-3:],squares[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "172519d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "9\n",
      "16\n"
     ]
    }
   ],
   "source": [
    "#对切片进行遍历\n",
    "for val in squares[1:4]:\n",
    "    print(val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "070fabc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['bmw', 'audi', 'toyota', 'benz', 'subaru'] \n",
      "\n",
      "['bmw', 'audi', 'toyota', 'benz'] ['bmw', 'audi', 'toyota', 'benz', 'subaru']\n"
     ]
    }
   ],
   "source": [
    "#利用切片进行列表的复制\n",
    "cars = ['bmw','audi','toyota','benz']\n",
    "my_cars = cars\n",
    "my_cars.append('subaru')\n",
    "print(cars,'\\n')#cars也增加了\n",
    "\n",
    "\n",
    "cars = ['bmw','audi','toyota','benz']\n",
    "your_cars = cars[:]\n",
    "your_cars.append('subaru')\n",
    "print(cars,your_cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "052f6989",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True True\n",
      "False True\n"
     ]
    }
   ],
   "source": [
    "###################################################流程处理\n",
    "#and or; in\n",
    "age=18\n",
    "print((age>15) and (age<20) ,(age>15) or (age>20))\n",
    "\n",
    "cars = ['bmw','audi','toyota','benz']\n",
    "car = 'beijing'\n",
    "print(car in cars, car not in cars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "709cabf1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age>14 and age<20\n"
     ]
    }
   ],
   "source": [
    "age=18\n",
    "if age<14:\n",
    "    print(\"age < 14\")\n",
    "elif age>14 and age<20:\n",
    "    print(\"age>14 and age<20\")\n",
    "else:\n",
    "    print(\">20\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "335cf5bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'color': 'green', 'points': 5, 'x_pos': 0, 'y_pos': 25}\n",
      "{'points': 5, 'x_pos': 0, 'y_pos': 25}\n"
     ]
    }
   ],
   "source": [
    "##########################################################字典\n",
    "alien = {'color':'green', 'points':5}\n",
    "alien['x_pos'] = 0\n",
    "alien['y_pos'] = 25\n",
    "print(alien)\n",
    "del alien['color']\n",
    "print(alien)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5dbd84f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "points 5\n",
      "x_pos 0\n",
      "y_pos 25\n",
      "\n",
      "\n",
      "points\n",
      "x_pos\n",
      "y_pos\n",
      "0\n",
      "25\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "#遍历\n",
    "for k, v in alien.items():\n",
    "    print(k,v)\n",
    "    \n",
    "print('\\n')\n",
    "#遍历所有键\n",
    "for k in alien.keys():\n",
    "    print(k)\n",
    "    \n",
    "#遍历所有值\n",
    "for v in set(alien.values()):\n",
    "    print(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "88de820c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Verifying user:Candace\n",
      "Verifying user:Brian\n",
      "Verifying user:Alice\n",
      "\n",
      "The following users have been confirmed:\n",
      "Candace\n",
      "Brian\n",
      "Alice\n"
     ]
    }
   ],
   "source": [
    "##########################################################循环\n",
    "# 列表做判断条件，代表不为空\n",
    "\n",
    "# 首先，创建一个待验证用户列表\n",
    "# 和一个用于存储已验证用户的空列表\n",
    "unconfirmed_users = ['alice','brian','candace']\n",
    "confirmed_users = []\n",
    "# 验证每个用户，直到没有未验证用户为止\n",
    "#  将每个经过验证的列表都移到已验证用户列表中\n",
    "while unconfirmed_users:\n",
    "    current_user = unconfirmed_users.pop() \n",
    "    print(\"Verifying user:\"+current_user.title())\n",
    "    confirmed_users.append(current_user) \n",
    "# 显示所有已验证的用户\n",
    "print(\"\\nThe following users have been confirmed:\")\n",
    "for confirmed_user in confirmed_users:\n",
    "    print(confirmed_user.title())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4fe53f6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']\n",
      "['dog', 'dog', 'goldfish', 'rabbit']\n"
     ]
    }
   ],
   "source": [
    "#循环删除\n",
    "pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']\n",
    "print(pets)\n",
    "while 'cat'in pets:\n",
    "    pets.remove('cat')\n",
    "print(pets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "177d5a8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'}\n"
     ]
    }
   ],
   "source": [
    "##########################################函数\n",
    "\n",
    "#**形参，传入键值对，用列表的形式去访问\n",
    "def build_profile(first,last,**user_info):\n",
    "    \"\"\"创建一个字典，其中包含我们知道的有关用户的一切\"\"\"\n",
    "    profile = {}\n",
    "    profile['first_name'] = first\n",
    "    profile['last_name'] = last\n",
    "    for key,value in user_info.items():\n",
    "        profile[key] = value\n",
    "    return profile\n",
    "user_profile = build_profile('albert','einstein',\n",
    "                             location='princeton',\n",
    "                             field='physics')\n",
    "print(user_profile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8df86450",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "lw 40 black\n",
      "dog:lw barked.\n",
      "41\n"
     ]
    }
   ],
   "source": [
    "#################################################类\n",
    "class Dog():\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.color = 'black' #默认参数，此时无需指定形参\n",
    "    def bark(self):\n",
    "        print(\"dog:\" + self.name + \" barked.\")\n",
    "    def setAge(self, age):\n",
    "        self.age = age\n",
    "        \n",
    "my_dog = Dog(\"lw\", 40)  \n",
    "print(my_dog.name, my_dog.age, my_dog.color)\n",
    "my_dog.bark()\n",
    "my_dog.setAge(41)\n",
    "print (my_dog.age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "1dbd134b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "teslamodel s2023\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "#继承\n",
    "class Car():\n",
    "    def __init__(self, brand, model, year):\n",
    "        self.brand = brand\n",
    "        self.model = model\n",
    "        self.year = year\n",
    "        self.miles = 0\n",
    "    def set_miles(self,miles):\n",
    "        self.miles = miles\n",
    "    def get_miles(self):\n",
    "        return self.miles\n",
    "    def msg(self):\n",
    "        return self.brand+self.model+str(self.year)\n",
    "    \n",
    "class ElectricCar(Car):\n",
    "    def __init__(self, brand, model, year):\n",
    "        super().__init__(brand, model, year)\n",
    "        self.battery = 100\n",
    "        \n",
    "    def desc_battery(self):\n",
    "        return self.battery\n",
    "    \n",
    "car = Car('tesla', 'model s', 2023)\n",
    "print(car.get_miles())\n",
    "my_tesla = ElectricCar('tesla', 'model s', 2023)\n",
    "print(my_tesla.msg())\n",
    "print(my_tesla.desc_battery())\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "5f86f48c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1415926535\n",
      "  8979323846\n",
      "  2643383279\n",
      "yes! 3.1415926535\n",
      "\n",
      "yes!   8979323846\n",
      "\n",
      "yes!   2643383279\n"
     ]
    }
   ],
   "source": [
    "#################################################文件\n",
    "#with在读取结束后关闭文件\n",
    "with open('pi_digits.txt') as file_obj:\n",
    "    content = file_obj.read()\n",
    "    print(content)\n",
    "    \n",
    "with open('pi_digits.txt') as file_obj:    \n",
    "    for line in file_obj:\n",
    "        print('yes!', line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8a723582",
   "metadata": {},
   "outputs": [],
   "source": [
    "#以写入('w')模式打开文件时如果指定的文件已经存在，将在返回文件对象前清空该文件\n",
    "filename = 'programming.txt'\n",
    "with open(filename,'w') as file_object:\n",
    "    file_object.write(\"I love programming.\") \n",
    "#使用追加模式\n",
    "filename = 'programming.txt'\n",
    "with open(filename,'a') as file_object:\n",
    "    file_object.write(\"I also love finding meaning in large datasets.\\n\") \n",
    "    file_object.write(\"I love creating apps that can run in a browser.\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f91f2b4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "#以json存储\n",
    "import json\n",
    "\n",
    "nums = [1, 2, 3, 4, 5]\n",
    "with open('numbers.json', 'w') as file_obj:\n",
    "    json.dump(nums, file_obj)\n",
    "    \n",
    "numbers = []\n",
    "with open('numbers.json') as f_obj:\n",
    "    numbers = json.load(f_obj)\n",
    "    print(numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0dad912b",
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################d单元测试\n",
    "def get_formatted_name(first,last):\n",
    "    \"\"\"Generate a neatly formatted full name.\"\"\"\n",
    "    full_name = first+''+last\n",
    "    return full_name.title()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "aa50bf16",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E\n",
      "======================================================================\n",
      "ERROR: C:\\Users\\13628\\AppData\\Roaming\\jupyter\\runtime\\kernel-60964b12-fd17-4bb3-ae23-df989aff0cf4 (unittest.loader._FailedTest)\n",
      "----------------------------------------------------------------------\n",
      "AttributeError: module '__main__' has no attribute 'C:\\Users\\13628\\AppData\\Roaming\\jupyter\\runtime\\kernel-60964b12-fd17-4bb3-ae23-df989aff0cf4'\n",
      "\n",
      "----------------------------------------------------------------------\n",
      "Ran 1 test in 0.001s\n",
      "\n",
      "FAILED (errors=1)\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "True",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m True\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\conda_env\\envs\\d2l\\lib\\site-packages\\IPython\\core\\interactiveshell.py:3558: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "import unittest\n",
    "# from name_function import get_formatted_name\n",
    "class NamesTestCase(unittest.TestCase):\n",
    "    \"\"\"测试name_function.py\"\"\"\n",
    "    def test_first_last_name(self):\n",
    "        formatted_name = get_formatted_name('janis','joplin') \n",
    "        self.assertEqual(formatted_name,'janis joplin') \n",
    "unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4740d988",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
